package com.dreamoo.bloomFilter;

import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RBloomFilter;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Arrays;

/**
 * @author 山顶洞人郑某
 * @className UserBloomFilter.java
 * @date 2025 05 09
 */
@Component
@Slf4j
public class UserBloomFilter {

    private final RBloomFilter<String> userBloomFilter;

    public UserBloomFilter(RedissonClient redissonClient,BloomFilterProperties bloomFilterProperties) {
        userBloomFilter = redissonClient.getBloomFilter(bloomFilterProperties.getName());
        userBloomFilter.tryInit(bloomFilterProperties.getExpectedInsertions(), bloomFilterProperties.getFalseProbability());
        log.info("初始化用户布隆过滤器成功:{}", userBloomFilter);
    }

    public boolean contains(String userName) {
        return userBloomFilter.contains(userName);
    }

    public void add(String userName) {
        userBloomFilter.add(userName);
    }

    public void add(String... userNames) {
        userBloomFilter.add(Arrays.asList(userNames));
    }

    public void clear() {
        userBloomFilter.delete();
    }

}
